﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.MEP;

namespace YArchitech.Plumbing
{
	public class RiserHeatingPipeOperation
	{
		public bool SetParament(ref double zStart, ref double zEnd, ref PipeType pipeTypeRiser, ref PipingSystemType pipingSystemTypeRiser, ref string diameter, FormReiseDrawHeatingPipe drawPipeForm)
		{
			zStart = drawPipeForm.zStart;
			zEnd = drawPipeForm.zEnd;
			pipeTypeRiser = drawPipeForm.pipeTypeRiser;
			pipingSystemTypeRiser = drawPipeForm.pipingSystemTypeRiser;
			diameter = drawPipeForm.diameter;
			if (drawPipeForm.hySystemType.currentSystemTypeInfo == null || drawPipeForm.hySystemType.currentSystemTypeInfo.SysType == null || drawPipeForm.hySystemType.currentSystemTypeInfo.PipeType == null)
			{
				YJKMessageBox.Show("需要选择系统类型");
				return false;
			}
			return true;
		}

		public void SetMark(Pipe RiserPipe, UIDocument uidoc, FormReiseDrawHeatingPipe drawPipeForm, UIApplication uiApp, AuxiliaryFrameOperation afOperation)
		{
			using (CreateRiserHeatingPipeLinesJig createRiserHeatingPipeLinesJig = new CreateRiserHeatingPipeLinesJig(uidoc, drawPipeForm))
			{
				RiserPipeOperation riserPipeOperation;
				RiserPipeAndCenter riserPipeAndCenter;
				XYZ textLocation;
				XYZ subVector;
				XYZ xyz;
				string empty;
				string empty2;
				for (;;)
				{
					riserPipeOperation = new RiserPipeOperation();
					XYZ riserPipeCenter = riserPipeOperation.GetRiserPipeCenter(RiserPipe, uidoc);
					riserPipeAndCenter = new RiserPipeAndCenter(RiserPipe, riserPipeCenter);
					createRiserHeatingPipeLinesJig.StartPoint = riserPipeCenter;
					textLocation = null;
					subVector = null;
					xyz = uidoc.Selection.PickPoint("请选择标注位置：");
					if (createRiserHeatingPipeLinesJig.StartPoint.IsAlmostEqualTo(xyz, 0.001))
					{
						YJKMessageBox.Show("选择标注位置不合适，请重新选取！");
					}
					else
					{
						empty = string.Empty;
						empty2 = string.Empty;
						if (this.setPipeParameter(ref empty, ref empty2, drawPipeForm))
						{
							break;
						}
					}
				}
				MarkRiserPipeUtility.SetRiserPipeParameters(uiApp, riserPipeAndCenter.RiserPipe, empty, empty2, true, false);
				double height = 0.0;
				double lineLength = riserPipeOperation.getLineLength(uidoc, riserPipeAndCenter, ref height);
				double angle = 0.0;
				bool isCustomAngle = false;
				try
				{
					angle = Convert.ToDouble(drawPipeForm.hyTxtAngle.Text);
					isCustomAngle = drawPipeForm.chkLineAngle.Checked;
				}
				catch (Exception)
				{
					angle = 45.0;
					isCustomAngle = false;
				}
				bool isleft = xyz.X < createRiserHeatingPipeLinesJig.StartPoint.X;
				List<Line> list = MarkRiserPipeUtility.DrawSinglePipeMarkLines(createRiserHeatingPipeLinesJig.StartPoint, xyz, angle, isCustomAngle, Math.Abs(AssistFunc.feetToMM(xyz.Y - createRiserHeatingPipeLinesJig.StartPoint.Y)), lineLength, ref textLocation, ref subVector, uidoc.Document, isleft);
				DetailCurveArray detailCurveArray = new DetailCurveArray();
				foreach (Curve curve in list)
				{
					DetailCurve detailCurve = uidoc.Document.Create.NewDetailCurve(uidoc.ActiveView, curve);
					detailCurveArray.Append(detailCurve);
				}
				if (detailCurveArray != null)
				{
					Autodesk.Revit.DB.Group group = this.createGroup(detailCurveArray, uidoc, riserPipeAndCenter, afOperation, RiserPipe);
					IndependentTag independentTag = null;
					MarkRiserPipeUtility.MarkSinglePipeText(uidoc.Document, uidoc.Document.ActiveView, riserPipeAndCenter.RiserPipe, textLocation, subVector, lineLength, false, false, group, height, ref independentTag);
					new List<Pipe>().Add(riserPipeAndCenter.RiserPipe);
					drawPipeForm.hyTextBox1.Text = this.AddPipeNumber(empty2);
					uidoc.Document.Regenerate();
				}
			}
		}

		private string AddPipeNumber(string pipeNumber)
		{
			char[] array = pipeNumber.ToCharArray();
			string text = "";
			string text2 = "";
			bool flag = true;
			for (int i = array.Count<char>() - 1; i >= 0; i--)
			{
				if (flag)
				{
					if (char.IsDigit(array[i]))
					{
						text = array[i].ToString() + text;
					}
					else
					{
						text2 = array[i].ToString() + text2;
						flag = false;
					}
				}
				else
				{
					text2 = array[i].ToString() + text2;
				}
			}
			if (text == "")
			{
				return pipeNumber;
			}
			pipeNumber = (text.ToInt(0) + 1).ToString();
			pipeNumber = text2 + pipeNumber;
			return pipeNumber;
		}

		private bool setPipeParameter(ref string areaAndFloor, ref string customNumber, FormReiseDrawHeatingPipe drawPipeForm)
		{
			string str = string.Empty;
			string str2 = string.Empty;
			if (drawPipeForm.hyTextBox1.Text == "" || !Regex.IsMatch(drawPipeForm.hyTextBox1.Text, "^[A-Za-z0-9'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				drawPipeForm.hyTextBox1.Focus();
				return false;
			}
			if (drawPipeForm.District)
			{
				str = drawPipeForm.DistrictNum + "-";
			}
			if (drawPipeForm.Build)
			{
				str2 = drawPipeForm.BuildNum + "-";
			}
			areaAndFloor = str + str2;
			customNumber = drawPipeForm.hyTextBox1.Text;
			return true;
		}

		private Autodesk.Revit.DB.Group createGroup(DetailCurveArray detailCurveArr, UIDocument uidoc, RiserPipeAndCenter pipeAndCenter, AuxiliaryFrameOperation afOperation, Pipe RiserPipe)
		{
			List<RiserPipeAndCenter> list = new List<RiserPipeAndCenter>();
			list.Add(pipeAndCenter);
			Autodesk.Revit.DB.Group group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(uidoc.Document, list, detailCurveArr);
            AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
			afOperation.SetAuxiliaryFramePipeGroup(uidoc.Document, RiserPipe, group);
			return group;
		}
	}
}
